package com.esms.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import com.esms.service.ILogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.esms.dao.EmployeeMapper;
import com.esms.po.Aassessment;
import com.esms.po.Employee;
import com.esms.po.Grand;
import com.esms.service.impl.EmployeeGrandServerImpl;
import com.esms.vo.EmployeeGrandVO;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 
 * @author 帅帅帅帅
 *
 */

@Controller
public class EmployeeGrandController {

	@Autowired
	private ILogService logService;
	
	@Autowired
	private EmployeeGrandServerImpl EmployeeGrandService;
	
	@Autowired
    private EmployeeMapper employeeMapper = null;
	
    @RequestMapping("employeeGrand.do")
    @ResponseBody
    public EmployeeGrandVO findSelectiveGrand(
            @RequestParam(value="e_id", defaultValue="0") String fEid,
            HttpSession httpSession) throws Exception {

        Integer eId = (Integer) httpSession.getAttribute("employeeId");
        //pageNum:起始页面  pageSize:每页的大小
      //  PageHelper.startPage(pageNum,limit);
        //查找条件，一定要紧跟在startPage后
        
        List<Grand> grandList = EmployeeGrandService.getEmployeeGrandList(eId,fEid);

        //设置前台需要的数据
        EmployeeGrandVO employeeGrandVO = new EmployeeGrandVO();
        employeeGrandVO.setCode(0);
        employeeGrandVO.setMsg("");
        employeeGrandVO.setCount(grandList.size());
        employeeGrandVO.setData(grandList);
        return employeeGrandVO;
    }
    
    @RequestMapping("updateGrand.do")
    @ResponseBody
    public EmployeeGrandVO updateGrand(
    		@RequestParam("list") String list,
            HttpSession httpSession) throws Exception {
    	int req = 0;
    	String reMsg;
    	ObjectMapper mapper = new ObjectMapper();
    	JavaType jt = mapper.getTypeFactory().constructParametricType(ArrayList.class, Grand.class);
    	List<Grand> listGrand =  (List<Grand>)mapper.readValue(list, jt);
    	/*我的工号查询*/
    	Integer eId = (Integer) httpSession.getAttribute("employeeId");
    	Employee employee = employeeMapper.selectByPrimaryKey(eId);
    	reMsg = "评价成功";
		logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
				"同级评价成功");
    	//获取系统当前时间
    	String quarter = null;
        String systemTime = new SimpleDateFormat("yyyyMMddHHmmss").format(new Date(System.currentTimeMillis()));
        int year = Calendar.getInstance().get(Calendar.YEAR);
        int month = Calendar.getInstance().get(Calendar.MONTH)+1;
        if(month>=1 && month<=3) {
        	quarter = year + "01";
        }else if (month>=4 && month<=6) {
        	quarter = year + "02";
        }else if (month>=7 && month<=9) {
        	quarter = year + "03";
        }else if (month>=10 && month<=12) {
        	quarter = year + "04";
        }
        Aassessment aassessment = new Aassessment();
    	for(int i=0;i<listGrand.size();i++) {
    		Grand grandData = listGrand.get(i);
    		/*输入分数判断*/
    		
    		/*设置同级评分表*/
    		aassessment.setaAuserid(employee.geteAccount());
    		aassessment.setaDuserid(grandData.geteAccount());
    		aassessment.setaCreateTime(systemTime);
    		aassessment.setaCreateQuarter(quarter);
    		aassessment.setaTargetid1(1);
    		aassessment.setaTargetid2(2);
    		aassessment.setaTargetid3(3);
    		aassessment.setaTargetid4(4);
    		aassessment.setaTargetid5(5);
    		aassessment.setaTargetid6(6);
    		aassessment.setaTargetid7(7);
    		aassessment.setaTargetid8(8);
    		aassessment.setaTargetid9(9);
    		aassessment.setaTargetid10(10);
    		aassessment.setaTargetid11(11);
    		aassessment.setaTargetid12(12);
    		aassessment.setaTargetid13(13);
    		aassessment.setaTargetid14(14);
    		aassessment.setaTargetid15(15);
    		aassessment.setaTargetid16(16);
    		aassessment.setaTargetid17(17);
    		aassessment.setaTargetid18(18);
    		aassessment.setaTargetid19(19);
    		aassessment.setaTargetid20(20);
    		aassessment.setaTargetscore1(grandData.getaTargetscore1());
    		aassessment.setaTargetscore2(grandData.getaTargetscore2());
    		aassessment.setaTargetscore3(grandData.getaTargetscore3());
    		aassessment.setaTargetscore4(grandData.getaTargetscore4());
    		aassessment.setaTargetscore5(grandData.getaTargetscore5());
    		aassessment.setaTargetscore6(grandData.getaTargetscore6());
    		aassessment.setaTargetscore7(grandData.getaTargetscore7());
    		aassessment.setaTargetscore8(grandData.getaTargetscore8());
    		aassessment.setaTargetscore9(grandData.getaTargetscore9());
    		aassessment.setaTargetscore10(grandData.getaTargetscore10());
    		aassessment.setaTargetscore11(grandData.getaTargetscore11());
    		aassessment.setaTargetscore12(grandData.getaTargetscore12());
    		aassessment.setaTargetscore13(grandData.getaTargetscore13());
    		aassessment.setaTargetscore14(grandData.getaTargetscore14());
    		aassessment.setaTargetscore15(grandData.getaTargetscore15());
    		aassessment.setaTargetscore16(grandData.getaTargetscore16());
    		aassessment.setaTargetscore17(grandData.getaTargetscore17());
    		aassessment.setaTargetscore18(grandData.getaTargetscore18());
    		aassessment.setaTargetscore19(grandData.getaTargetscore19());
    		aassessment.setaTargetscore20(grandData.getaTargetscore20());
    		int sum = grandData.getaTargetscore1() + grandData.getaTargetscore2()
    		 + grandData.getaTargetscore3() + grandData.getaTargetscore4()
    		 + grandData.getaTargetscore5() + grandData.getaTargetscore6() 
    		 + grandData.getaTargetscore7() + grandData.getaTargetscore8()
    		 + grandData.getaTargetscore9() + grandData.getaTargetscore10()
    		 + grandData.getaTargetscore11() + grandData.getaTargetscore12()
    		 + grandData.getaTargetscore13() + grandData.getaTargetscore14()
    		 + grandData.getaTargetscore15() + grandData.getaTargetscore16()
    		 + grandData.getaTargetscore17() + grandData.getaTargetscore18()
    		 + grandData.getaTargetscore19() + grandData.getaTargetscore20();
    		aassessment.setaTargetscoresum(sum);
    		
    		req = EmployeeGrandService.deleteGrand(employee.geteAccount(),grandData.geteAccount());
    		if(req < 0) {
       		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"同级评价失败");
    		}
    		req = EmployeeGrandService.insertGrand(aassessment);
    		if(req < 0) {
          		 reMsg = "评价失败";
				logService.addLog(this.getClass().getName(),Thread.currentThread().getStackTrace()[1].getMethodName(),(Integer)httpSession.getAttribute("employeeId"),
						"同级评价失败");
       		}
        }

        //设置前台需要的数据
        EmployeeGrandVO employeeGrandVO = new EmployeeGrandVO();
        employeeGrandVO.setCode(0);
        employeeGrandVO.setMsg(reMsg);
        employeeGrandVO.setData(listGrand);
        return employeeGrandVO;
    }
    public int checkNumber(Grand grand) {
    	return 0;
    }
}
